package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.MyYaoqingBean;
import com.wude.bean.UserInfo;
import com.wude.guild.adapter.MyYaoListAdapter;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("HandlerLeak")
public class MyInvitationActivity extends TitleBaseActivity {

    @BindView(R.id.ptb)
    TextView ptb;
    @BindView(R.id.ren)
    TextView ren;
    @BindView(R.id.listview)
    ListView listview;
    private MyYaoListAdapter myYaoListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_invitation);
        ButterKnife.bind(this);

        setHeaderTitle("我的邀请");
        myYaoListAdapter = new MyYaoListAdapter(this);
        listview.setAdapter(myYaoListAdapter);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(bhandler, HttpCom.getYaoURL(), map, false);
        initdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initdata() {
        UserInfo loginUser = Utils.getLoginUser();
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        HttpCom.POST(handler, HttpCom.getMyYaoqingURL(), map, false);
    }

    /**
     * 我的邀请
     */

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    List<MyYaoqingBean> myYaoqingBeen = HttpUtils.DNSMyYaoqing(msg.obj.toString());
                    if (myYaoqingBeen != null) {
                        myYaoListAdapter.setList(myYaoqingBeen);
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    Handler bhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("用户分享json", msg.obj.toString());
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        if (status == 1) {
                            JSONObject data = jsonObject.getJSONObject("data");
                            String invite_num = data.getString("invite_num");
                            String award_coin = data.getString("award_coin");
                            ptb.setText(award_coin);
                            ren.setText(invite_num);
                        } else {
                            String return_code = jsonObject.getString("return_code");
                            ToastUtil.showToast(return_code);
                            ptb.setText("0");
                            ren.setText("0");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("解析用户分享出错", e.toString());
                        ptb.setText("0");
                        ren.setText("0");
                    }
                    break;
                case 2:
                    ptb.setText("0");
                    ren.setText("0");
                    break;
            }
        }
    };

}
